﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using MVCWeb.Models;

namespace MVCWeb.Controllers
{

    [HandleError]
    public class AccountController : Controller
    {

        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

    // **************************************
    // URL: /Account/LogOn
    // **************************************

    public ActionResult LogOn()
    {
        return View();
    }
     /*
        LogOn:
      *  Login: Email 
      *  Password: FirstName 
      */
    [HttpPost]
    public ActionResult LogOn(CustomerModel model, string returnUrl)
    {
        CustomerModel PLCustomer = new Models.CustomerModel();
        int id = 0;
        PLCustomer =  CustomerClientServices.GetLogInfor(model.email, model.first_name);
       // if (ModelState.IsValid)
     //   {

        if (PLCustomer.email == null)
        {
            ModelState.AddModelError("", "Please enter email address.");
        }
            else if (PLCustomer.email != model.email)
            {
                ModelState.AddModelError("", "The Email was wrong.");   
            }
        else if (PLCustomer.first_name != model.first_name)
            {
                ModelState.AddModelError("", "The First name was wrong.");  
            }
        else if ((PLCustomer.email != model.email) && (PLCustomer.first_name != model.first_name))
            {
                ModelState.AddModelError("", "The Email and First was wrong."); 
            }
            else if(PLCustomer.blockuser == 1)
            {
                ModelState.AddModelError("" , "Sorry Your account was blocked by Web Masters");
            }
            else //if (MembershipService.ValidateUser(model.Email, model.FirstName))
            {
                FormsService.SignIn(model.email, model.RememberMe);
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                if(PLCustomer.permission == 1)
                {
                    id = Models.CustomerClientServices.GetIdFromEmail(model.email);
                    return RedirectToAction("WebMasterPage", "Webmaster", new { custid = id });
               //     return RedirectToAction("WebMasterPage", "Webmaster");
                
                }
                else
                {
                    id = Models.CustomerClientServices.GetIdFromEmail(model.email);
                    return RedirectToAction("Account", "Home", new { custid = id}); // there is no ACCOUNT file!!!!!!! I donno what page use for normal use!!!!
                }
            }
      /*      else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }*/
         
    //    }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

    // **************************************
    // URL: /Account/LogOff
    // **************************************

    public ActionResult LogOff()
    {
        FormsService.SignOut();

        return RedirectToAction("LogOn", "Account");
    }

    // **************************************
    // URL: /Account/Register
    // **************************************

   public ActionResult Register()
    {
       // ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        return View();
   }

    [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
          //  MembershipCreateStatus createStatus = MembershipService.CreateUser(model.Email, model.FirstName, model.LastName , model.Address ,
           //                                        model.State , model.Zip ,model.City );

      //      if (createStatus == MembershipCreateStatus.Success)
      //      {
            //    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
       //         return RedirectToAction("Index", "Home");
       //     }
      //      else
      //      {
       //         ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
        //    }
    }

// If we got this far, something failed, redisplay form
      //  ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        return View(model);
     }

// **************************************
// URL: /Account/ChangePassword
// **************************************

[Authorize]
    public ActionResult ChangePassword()
    {
   //     ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        return View();
       }

  /*  [Authorize]
    [HttpPost]
    public ActionResult ChangePassword(ChangePasswordModel model)
    {
        if (ModelState.IsValid)
        {
            if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
            {
                return RedirectToAction("ChangePasswordSuccess");
            }
            else
            {
                ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
            }
    }

// If we got this far, something failed, redisplay form
        ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
        return View(model);
     }*/

// **************************************
// URL: /Account/ChangePasswordSuccess
// **************************************

    public ActionResult ChangePasswordSuccess()
    {
            return View();
    }


    }
}

